<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/12
 * Time: 10:43
 */
namespace Service\Controller;
use Admin\Controller\AdminController;

class OrderController extends AdminController
{
    protected $modelName = 'Order';

    protected $searchRule = array(
        'status'    =>  array('/\d/','eq','param1','param2'),
        'DATA_11'    =>  array('/1\d{12}/','eq','param1','param2'),
        'DATA_8'    =>  array('/\D+/','like','%','%'),
    );

    public function create()
    {
        $req = $this->getReq();
        $type = array(
            array('maintain1','室内维修',),
            array('maintain2','公共维修',),
            array('maintain3','所有服务',),
            array('maintain4','洗衣服务',),
            array('maintain5','巡更服务',),
            array('maintain6','不正常服务',),
            array('maintain7','投诉服务',),
            array('maintain8','其他服务',),
        );
        $where['USER_PRIV'] = array(62, 65, 'or');
        $css = D('User')->where($where)->select();
        $communities = C('communities');
        $customer = D('Customer')->relation('families')->find($req['id']);
        $this->assign('type',$type);
        $this->assign('css',$css);
        $this->assign('communities',$communities);
        $this->assign('customer',$customer);
        $this->display();
    }

    public function orderList(){
        $this->display();
    }

    public function prcsInfo($id){
//        $run_id = I('param.')['RUN_ID'];
        $run_id = D('Crm/Order')->find($id)['RUN_ID'];
        $data = file_get_contents('http://oa.sxqtwy.com:88/interface/workflow_prcs.php?USER_ID=crm&PASSWORD=crm123&RUN_ID='.$run_id);
        $prcs = json_decode($data,true);
//        dump($prcs);exit;
        $record = array();
        foreach($prcs as $k=>$v){
            if($v['PRCS_ID'] == '1' && $v['PRCS_FLAG'] > 2){
                $record[0] = '('.$v['DEPT_NAME'].'|'.$v['USER_NAME'].')创建了工单，时间：'.$v['CREATE_TIME'];
                if($v['PRCS_FLAG'] > 3){
                    $record[1] = '('.$v['DEPT_NAME'].'|'.$v['USER_NAME'].')移交了工单，时间：'.$v['DELIVER_TIME'];
                }
            }elseif($v['PRCS_ID'] == '2' && $v['PRCS_TIME'] != '0000-00-00 00:00:00'){
                $record[2] = '('.$v['DEPT_NAME'].'|'.$v['USER_NAME'].')接收了工单，时间：'.$v['PRCS_TIME'];
                if($v['PRCS_FLAG'] > 3){
                    $record[3] = '('.$v['DEPT_NAME'].'|'.$v['USER_NAME'].')移交了工单，时间：'.$v['DELIVER_TIME'];
                }
            }elseif($v['PRCS_ID'] == '3' && $v['PRCS_FLAG'] > 2){
                $record[4] = '('.$v['DEPT_NAME'].'|'.$v['USER_NAME'].')接收了工单，时间：'.$v['PRCS_TIME'];
                $record[5] = '('.$v['DEPT_NAME'].'|'.$v['USER_NAME'].')移交了工单，时间：'.$v['DELIVER_TIME'];
            }elseif($v['PRCS_ID'] == '4' && $v['PRCS_FLAG'] > 2){
                $record[6] = '('.$v['DEPT_NAME'].'|'.$v['USER_NAME'].')接收了工单，时间：'.$v['PRCS_TIME'];
                if($v['PRCS_FLAG'] > 3){
                    $record[7] = '('.$v['DEPT_NAME'].'|'.$v['USER_NAME'].')办结了工单，时间：'.$v['DELIVER_TIME'];
                }
            }
        }
        ksort($record);
        return $record;
//        dump($record1);
//        dump($record2);
//        dump($record3);
//        dump($record4);
//        dump($record5);
//        dump($record6);
//        dump($record7);
//        dump($record8);
        exit;
        $this->ajaxReturn($record);
        exit;
    }

    public function search(){
        $param = $this->getParam();
        $modelName = $this->modelName;
        $scope = array();
        $relation = array();
        $search = $this->getSearch();
        $page = $this->getPage();
        $data = D($modelName)->where($param)->where($search)->scope($scope)->relation($relation)->page($page[0],$page[1])->select();
        if($data){
            $res['stat'] = 1;
            $res['data'] = array_change_key_case($data,CASE_LOWER);
            $res['count'] = $data = D($modelName)->where($param)->where($search)->scope($scope)->count();
            $res['msg'] = '查找到1条数据。';
        }elseif($data == null){
            $res['stat'] = 1;
            $res['data'] = array();
            $res['count'] = 0;
            $res['msg'] = '查找到0条数据。';
        }else{
            $res['stat'] = 0;
            $res['data'] = $data;
            $res['msg'] = '查找失败！';
        }
        $res['recordsTotal'] = $res['count'];
        $res['recordsFiltered'] = $res['count'];
        $this->ajaxReturn($res);
    }

    public function info(){
        $param = $this->getParam();
        if($param){
            $modelName = $this->modelName;
            $pk = D($modelName)->getPk();
            $field = array();
            $relation = array();
            if($param[$pk]){
                $data = D($modelName)->field($field)->relation($relation)->find($param[$pk]);
            }else{
                $data = D($modelName)->where($param)->field($field)->relation($relation)->find();
            }
        }
        $record = $this->prcsInfo($param['ID']);
        $this->assign('data',$data);
        $this->assign('record',$record);
        // dump($record);
        $this->display();
    }

    public function send(){
        $req = $this->getReq();

        $user1 = D('User')->where(array('crm_id'=>getUserId()))->find();
        $user2 = D('User')->where(array('DEPT_ID'=>$req['TO_DEPT'],'USER_PRIV_OTHER'=>array('in',array(62,65))))->select();
        $customer = D('Admin/Customer')->find($req['customer_id']);

        //OA数据操作
        $user1_id = $user1['USER_ID'];
        // $user2_id = implode(',',array_column($user2,'USER_ID'));
        // $user2_id = implode(',', D('User')->field('USER_ID')->where(array('DEPT_ID'=>$req['TO_DEPT'],'USER_PRIV_OTHER'=>array('in',array(62,65))))->select());
        $user2_id = '';
        foreach ($user2 as $k => $v) {
            if($user2_id === ''){
                $user2_id .= $v['USER_ID'];
            }else{
                $user2_id .= ','.$v['USER_ID'];
            }
        }
        $flow_id = '152';

        $param = $req;
        $param['DATA_1'] = $user1['USER_NAME']; //客服名称
        $param['DATA_2'] = $user1['DEPT_NAME']; //客服部门
        $param['DATA_3'] = $user1['PRIV_NAME']; //客服职位
        $param['DATA_4'] = date('Y-m-d H:i:s'); //创建时间
//        $param['DATA_5'] = $tel; //来电号码
        $param['DATA_6'] = $param['DATA_6']; //客户姓名
        $param['DATA_7'] = $customer['sex'] == 'M' ? '男' : '女'; //客户性别
//        $param['DATA_8'] = //客户信息（家庭住址）
        $param['DATA_9'] = $param['DATA_9']; //大类
        $param['DATA_10'] = $param['DATA_10']; //小类
        $param['DATA_11'] = $param['DATA_11']; //维修时联系电话
        $param['DATA_12'] = $param['DATA_12']; //事件说明
        $str = '';
        foreach($param as $k=>$v){
            if(!($k == 'TO_DEPT' || $k == 'customer_id')){
                $str .= '&'.$k.'='.$v;
            }
        }
//        var_dump($str);exit;
        $query_url = 'http://oa.sxqtwy.com:88/interface/workflow1.php?USER_ID=crm&PASSWORD=crm123&FLOW_ID='.$flow_id.'&USER_ID1='.$user1_id.'&USER_ID2='.$user2_id.$str;
//        $query_url = 'http://oa.sxqtwy.com:88/interface/workflow1.php?USER_ID=crm&PASSWORD=crm123&FLOW_ID='.$flow_id.'&USER_ID1='.$user1_id.'&USER_ID2=91,20'.$str;

        //CRM数据操作
        $result = json_decode(file_get_contents($query_url),true);
        $param['BEGIN_USER'] = $user1_id; //客服ID
        $param['RUN_ID'] = $result['RUN_ID']; //流程实例ID
        $param['FLOW_ID'] = $result['FLOW_ID']; //流程ID
        $param['PRCS_KEY_ID'] = $result['PRCS_KEY_ID']; //流程步骤ID
        $param['create_time'] = time();
        $data = D('Order')->data($param)->add();
        if($data && $param['RUN_ID']){
            $res['stat'] = 1;
            $res['msg'] = '创建订单成功';
        }else{
            $res['stat'] = 0;
            $res['msg'] = '创建订单失败';
        }
        $this->ajaxReturn($res);
//        echo D('Order')->getLastSql();
//        echo D('Order')->getDbError();
    }
}